package com.itheima.student.system06.entry;

import com.itheima.student.system06.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentEntry {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("--------欢迎使用学生管理系统---------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    findStudent(list);
                    break;
                case "5":
                    System.out.println("谢谢您的使用!");
                    break loop;
                default:
                    System.out.println("您的输入有误,请重新输入:");
                    break;
            }
        }

    }
        //  添加学生
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要录入的学生的学号:");
        //  此处需要判断该学号系统中是否已存在
        String sid = null;
        while (true) {
            sid = sc.next();
            int index = getIndex(sid, list);
            if (index>=0){
                System.out.println("系统中已存在此学号,请重新录入正确的学号:");

            }else{
                System.out.println("系统中没有此学号,请您继续后续操作:");
                break;
            }
        }

        Student student = getStudent(sc, sid);
        list.add(student);
        System.out.println("录入成功!");
    }
        //  删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        //  先判断系统中是否有学生信息
        if (list.size()==0){
            System.out.println("系统中没有学生信息,请录入!");
        }
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要删除的学生的学号:");
            String sid = sc.next();
            int index = getIndex(sid, list);
            if (index>=0){
                list.remove(index);
                System.out.println("删除成功!");
                break;
            }else{
                System.out.println("系统中没有此学生,无需删除!");
                break;
            }
        }

    }
        // 修改学生信息
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要修改的学生的学号:");
            String sid = sc.next();
            int index = getIndex(sid, list);
            if (index>=0){
                System.out.println("请修改其信息:");;
                Student student = getStudent(sc, sid);
                list.set(index,student);
                System.out.println("信息修改成功!");
                break;
            }else{
                System.out.println("系统中没有此学生,请输入正确的学号!");
                break;
            }
        }

    }
        //  查看学生信息
    public static void findStudent(ArrayList<Student> list) {
        if (list.size()==0){
            System.out.println("系统中没有学生信息,无法查看!");
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("1.查看所有学生信息:");
        System.out.println("2.查看指定学生信息:");
        System.out.println("请输入你的选择:");
        String choice = sc.next();
        switch (choice){
            case "1":
                findAllStudents(list);
                break;
            case "2":
                Individualstudent(list);
                break;
            default:
                System.out.println("您的选择有误,请重新选择:");
                break;
        }
    }
        // 查看指定学生
    public static void Individualstudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要查看的学生的学号:");
        String sid = sc.next();
        int index = getIndex(sid, list);
        if (index>=0){
            Student student = list.get(index);
            System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "性别" + "\t" + "生日");
            System.out.println(student.getSid()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getGender()+"\t"+student.getBirthday());
        }
    }
            // 查看所有的学生信息
    public static void findAllStudents(ArrayList<Student> list) {
       System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "性别" + "\t" + "生日");
        for (int i =0; i < list.size(); i++) {
             // list.get(i)
            Student student = list.get(i);
            System.out.println(student.getSid()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getGender()+"\t"+student.getBirthday());
        }
    }
    public static Student getStudent(Scanner sc, String sid) {
        System.out.println("请输入你要录入的学生的姓名:");
        String name = sc.next();
        System.out.println("请输入你要录入的学生的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入你要录入的学生的性别:");
        String gender = sc.next();
        System.out.println("请输入你要录入的学生的生日:");
        String birthday = sc.next();
        Student student = new Student();
        student.setSid(sid);
        student.setName(name);
        student.setAge(age);
        student.setGender(gender);
        student.setBirthday(birthday);
        return student;
    }
    //  判断该学号系统中是否存在
    // 参数 输入的sid  已有的集合 ArrayList<Student> list
    //  返回值  int类型
    public static int   getIndex(String sid,ArrayList<Student> list){
        for (int i = 0; i < list.size(); i++) {
         //   list.get(i)
            Student student = list.get(i);
            if (student.getSid().equals(sid)){
                return i;
            }
        }
        return -1;
    }
}
